package cn.lg.soar.core.sn;

import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public interface SnElement {

	/**
	 * 获取元素key（确保唯一性）
	 * @return
	 */
	String getKey();

	/**
	 * 获取元素值
	 * @param args
	 * @return
	 */
	String getValue(Object...args);

	@Component
	class Year implements SnElement {
		@Override
		public String getKey() {
			return "year";
		}
		@Override
		public String getValue(Object... args) {
			return String.valueOf(LocalDate.now().getYear());
		}
	}

	@Component
	class Month implements SnElement {
		@Override
		public String getKey() {
			return "month";
		}
		@Override
		public String getValue(Object... args) {
			int month = LocalDate.now().getMonthValue();
			return month < 10 ? ("0" + month) : String.valueOf(month);
		}
	}

	@Component
	class Day implements SnElement {
		@Override
		public String getKey() {
			return "day";
		}
		@Override
		public String getValue(Object... args) {
			int day = LocalDate.now().getDayOfMonth();
			return day < 10 ? ("0" + day) : String.valueOf(day);
		}
	}

	@Component
	class Hour implements SnElement {
		@Override
		public String getKey() {
			return "hour";
		}
		@Override
		public String getValue(Object... args) {
			int hour = LocalTime.now().getHour();
			return hour < 10 ? ("0" + hour) : String.valueOf(hour);
		}
	}

	@Component
	class Minute implements SnElement {
		@Override
		public String getKey() {
			return "minute";
		}
		@Override
		public String getValue(Object... args) {
			int minute = LocalTime.now().getMinute();
			return minute < 10 ? ("0" + minute) : String.valueOf(minute);
		}
	}

	@Component
	class Second implements SnElement {
		@Override
		public String getKey() {
			return "second";
		}
		@Override
		public String getValue(Object... args) {
			int second = LocalTime.now().getSecond();
			return second < 10 ? ("0" + second) : String.valueOf(second);
		}
	}

	@Component
	class YyyyMM implements SnElement {
		private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
		@Override
		public String getKey() {
			return "yyyyMM";
		}
		@Override
		public String getValue(Object... args) {
			return formatter.format(LocalDate.now());
		}
	}

	@Component
	class YyyyMMdd implements SnElement {
		private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		@Override
		public String getKey() {
			return "yyyyMMdd";
		}
		@Override
		public String getValue(Object... args) {
			return formatter.format(LocalDate.now());
		}
	}

	@Component
	class YyyyMMddHH implements SnElement {
		private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHH");
		@Override
		public String getKey() {
			return "yyyyMMddHH";
		}
		@Override
		public String getValue(Object... args) {
			return formatter.format(LocalDateTime.now());
		}
	}

	@Component
	class YyyyMMddHHmm implements SnElement {
		private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
		@Override
		public String getKey() {
			return "yyyyMMddHHmm";
		}
		@Override
		public String getValue(Object... args) {
			return formatter.format(LocalDateTime.now());
		}
	}

	@Component
	class YyyyMMddHHmmss implements SnElement {
		private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
		@Override
		public String getKey() {
			return "yyyyMMddHHmmss";
		}
		@Override
		public String getValue(Object... args) {
			return formatter.format(LocalDateTime.now());
		}
	}

	@Component
	class Datetime implements SnElement {
		private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
		@Override
		public String getKey() {
			return "datetime";
		}

		/**
		 * 例：datetime 或 datetime('yyyyMMddHHmmss')
		 * 方法说明：datetime(format)
		 * @param format：格式
		 * @return
		 */
		@Override
		public String getValue(Object... args) {
			if (args.length == 0) {
				return formatter.format(LocalDateTime.now());
			} else {
				return DateTimeFormatter.ofPattern((String) args[0]).format(LocalDateTime.now());
			}
		}
	}

	@Component
	class Random implements SnElement {
		private final static java.util.Random random = new java.util.Random();
		@Override
		public String getKey() {
			return "random";
		}

		/**
		 * 例：random(4) 或 random(3,'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
		 * 方法说明：random(length, source)
		 * @param length：位数
		 * @param source：随机字符串源，默认：ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
		 * @return
		 */
		@Override
		public String getValue(Object... args) {
			int length = (int)args[0];
			String str;
			if (args.length == 2) {
				str = (String) args[1];
			} else {
				str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			}
			char[] source = str.toCharArray();
			char[] chars = new char[length];
			for (int i = 0; i < length; i++) {
				chars[i] = source[random.nextInt(10)];
			}
			return String.valueOf(chars);
		}
	}

	@Component
	class Number implements SnElement {
		@Override
		public String getKey() {
			return "number";
		}

		/**
		 * 例：number(4)
		 * 方法说明：number(length)
		 * @param length：位数
		 * @return
		 */
		@Override
		public String getValue(Object... args) {
			int length = (int)args[0];
			String s = String.valueOf(Math.random());
			return s.substring(2, 2 + length);
		}
	}

	@Component
	class Const implements SnElement {
		@Override
		public String getKey() {
			return "const";
		}
		@Override
		public String getValue(Object... args) {
			return args[0].toString();
		}
	}

}
